require "common/Define"
require "framework/BaseController"
require "FairyGUI"

TreeViewMain = BaseController:New({mControllerName = "TreeViewMain"});

function TreeViewMain.Main()
	-- body
	TreeViewMain:Awake();
end

function TreeViewMain:Awake()
	Application.targetFrameRate = 60;
	Stage.inst.onKeyDown:Add(self.OnKeyDown, self);

	-- attack pacakge, package in resource or assetbundle
	local packageName = "UI/TreeView";
	UIPackage.AddPackage(packageName);

	local componentName = "Main";
	self.mainComponent = UIPackage.CreateObject("TreeView", componentName).asCom;
	GRoot.inst:AddChild( self.mainComponent );

	--local go = GameObject();
	--local panel = go:AddComponent(typeof(UIPanel));
	--panel.container:AddChild(self.mainComponent);
	--GRoot.inst:AddChild( panel );

	-- self.mainComponent:SetSize(GRoot.inst.width, GRoot.inst.height);
	-- self.mainComponent:AddRelation(GRoot.inst, RelationType.Size);
	GRoot.inst:SetContentScaleFactor(1134, 640, FairyGUI.UIContentScaler.ScreenMatchMode.MatchWidthOrHeight);

	self:OnViewCreated(self.mainComponent);
end

function TreeViewMain:InitView()
	self._folderURL1 = "ui://TreeView/folder_closed";
	self._folderURL2 = "ui://TreeView/folder_opened";
	self._fileURL = "ui://TreeView/file";

	self._treeView = FairyGUI.TreeView( self.mainComponent:GetChild("tree").asList );
	self._treeView.onClickNode:Add(self.__clickNode, self);
	self._treeView.treeNodeRender = FairyGUI.TreeView.TreeNodeRenderDelegate( self.RenderTreeNode, self );

	local topNode = FairyGUI.TreeNode(true);
	topNode.data = "I'm a top node";
	self._treeView.root:AddChild(topNode);
	for i=0, 4, 1 do
		local node = FairyGUI.TreeNode(false);
		node.data = "Hello " .. i;
		topNode:AddChild(node);
	end

	local aFolderNode = FairyGUI.TreeNode(true);
	aFolderNode.data = "A folder node";
	topNode:AddChild(aFolderNode);
	for i=0, 4, 1 do
		local node = FairyGUI.TreeNode(false);
		node.data = "Good " .. i;
		aFolderNode:AddChild(node);
	end

	for i=0, 2, 1 do
		local node = FairyGUI.TreeNode(false);
		node.data = "World " .. i;
		topNode:AddChild(node);
	end 

	local anotherTopNode = FairyGUI.TreeNode(false);
	anotherTopNode.data = { "I'm a top node too", "ui://TreeView/heart" };
	self._treeView.root:AddChild(anotherTopNode);
end

function TreeViewMain:RenderTreeNode(node)
	local btn = node.cell;
	print("RenderTreeNode: ", type(node.data) == type(table));
	print("RenderTreeNode: ", node.data);
	if (node.isFolder) then 
		if (node.expanded) then
			btn.icon = self._folderURL2;
		else
			btn.icon = self._folderURL1;
		end
		btn.title = node.data;
	elseif (type(node.data) == type(table) ) then
		btn.icon = node.data[2];
		btn.title = node.data[1];
	else
		btn.icon = self._fileURL;
		btn.title = node.data;
	end
end

function TreeViewMain:__clickNode(context)
	local node = context.data;
	if (node.isFolder) then 
		node.expanded = not node.expanded;
	end
end

function TreeViewMain:OnKeyDown(context)
 	-- body
 	if (context.inputEvent.keyCode == KeyCode.Escape) then
		print "Exit Game!"
		Application.Quit();
	end
end